package cn.kshost.zqyg.common.service.interceptor;

import cn.kshost.zqyg.model.entity.user.UserInfo;
import cn.kshost.zqyg.utils.AuthContextUtil;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class UserLoginAuthInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从redis获取用户信息
        String userInfoJson= redisTemplate.opsForValue().get("user:login:" + request.getHeader("token"));
        //将用户信息存入threadlocal
        AuthContextUtil.setUserInfoValue(JSON.parseObject(userInfoJson,UserInfo.class));
        return true;
    }
}
